//
//  YDownloaderHelper.h
//  yuikemall
//
//  Created by yuike6 yuike6 on 12-3-8.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "header.h"

@class YDownloadObjectModel;
@class DownloadRequest;
@class YDownloaderTask;

@protocol YDownloaderTaskDelegate <NSObject>
@optional
- (void) didTaskReceiveDataFinish: (NSData *) theData       task:(YDownloaderTask *) task;
- (void) didTaskReceiveFilename: (NSString *) aName         task:(YDownloaderTask *) task;
- (void) taskDataDownloadFailed: (NSString *) reason        task:(YDownloaderTask *) task;
- (void) taskDataDownloadAtPercent: (NSNumber *) aPercent   task:(YDownloaderTask *) task;
@end

@interface YDownloaderHelper : YTaskPool<YDownloaderTaskDelegate>
{
    YFixSizeMutableDictionary * requestDictionary_;
    
    NSTimeInterval timeout;
    
}

@property (nonatomic, retain) YFixSizeMutableDictionary * requestDictionary;
@property (nonatomic, assign) NSTimeInterval timeout;

- (void) addDownloadRequest:(DownloadRequest *) request;

- (void) cancelDownloadRequest:(DownloadRequest *) request;
- (void) cancelDownloadRequestByName:(NSString *) requestName;

- (void) stopAllRequest;

- (void) setAllRequestPriorityLevel:(PriorityLevel) level;

- (void) stopRequestsWithLevel:(PriorityLevel) level;

- (void) setRequestPriorityLevel:(PriorityLevel) level withRequestName:(NSString *) requestName;
@end


@interface YDownloaderTask : YTask 
{
    NSURLResponse *response_;   
    DownloadRecvData * recvData_; 
    
	YDownloadObjectModel * downloadModel_;
	NSURLConnection *urlconnection_;
    
    id <YDownloaderTaskDelegate> delegate_;
    
    NSTimeInterval  timeout;
    
    NSURL * url;
   
}

@property (nonatomic, retain) NSURLResponse *response;
@property (nonatomic, retain) NSURLConnection *urlconnection;

@property (nonatomic, retain) DownloadRecvData *recvData;
@property (nonatomic, retain) YDownloadObjectModel *downloadModel;

@property (nonatomic, assign) id<YDownloaderTaskDelegate> delegate;
@property (nonatomic, assign) NSTimeInterval  timeout;

@property (nonatomic, retain) NSURL * url;


- (void) download:(YDownloadObjectModel *) downloadModel;

- (void) stopDownload;
- (void) clearRecvedData;
- (void) stopDownloadAndClearRecvedData;
@end




